package com.jiang.剑指Offer.树.NC267;

import com.jiang.剑指Offer.树.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/01/06 19:14
 */
public class Solution {
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        if (root == null) return list;
        Queue<TreeNode> q = new LinkedList<>();
        list.add(root.val);
        q.offer(root);
        while (!q.isEmpty()) {
            TreeNode temp = q.poll();
            if (temp.left != null) {
                list.add(temp.left.val);
                q.offer(temp.left);
            }
            if (temp.right != null) {
                list.add(temp.right.val);
                q.offer(temp.right);
            }
        }
        return list;
    }
}
